import java.util.Stack;

public class MySingleList {
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;

    // 打印所有节点信息
    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }

    // 得到链表的长度
    public int size(){
        ListNode cur = head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    // 查找关键字 key 是否在链表中
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    // 头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    // 尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(cur == null) {
            head = node;
            return;
        }
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }

    // 在任意位置插入
    public void addIndex(int index, int data){
        if(index < 0 || index > size()){
            throw new PosOutOfBoundsException(index + "插入位置不合法");
        }
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        ListNode cur = findIndexPrev(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }

    // 找到 index 下标的前一个节点
    private ListNode findIndexPrev(int index){
        ListNode cur = head;
        while(--index > 0){
            cur = cur.next;
        }
        return cur;
    }

    // 删除节点
    public void remove(int key){
        if(head == null){
            return;
        }
        // 单独删除头结点
        if(head.val == key){
            head = head.next;
            return;
        }
        ListNode prev = findPrev(key);
        if(prev == null){
            throw new RuntimeException("要删除的节点不存在！");
        }
        ListNode del = prev.next;
        prev.next = del.next;
    }

    // 删除所有值为 key 的节点
    public void removeAll(int key){
        if(head == null){
            return;
        }
        ListNode prev = head;
        ListNode cur = prev.next;
        while(cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev = cur;
                cur = cur.next;
            }
        }
        // 判断头结点
        if(head.val == key){
            head = head.next;
        }
    }

    // 找到 data 节点的前驱
    private ListNode findPrev(int key){
        ListNode cur = head;
        while(cur.next != null){
            if(cur.next.val == key){
                return cur;
            }
        }
        return null;
    }

    // 清空链表
    public void clear(){
        this.head = null;
    }

    // 返回链表倒数第 k 个节点
    public ListNode findKthToTail(ListNode head, int k){
        if(head == null) return null;
        if(k <= 0 || k > size()) return null;
        ListNode fast = head;
        ListNode slow = head;
        while(--k > 0){
            fast = fast.next;
        }
        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    public ListNode partition(ListNode pHead, int x) {
        if(pHead == null) return null;
        ListNode head1 = new ListNode(0);
        ListNode head2 = new ListNode(0);
        ListNode cur1 = head1;
        ListNode cur2 = head2;
        ListNode cur = pHead;
        while(cur != null){
            if(cur.val < x){
                cur1.next = cur;
                cur1 = cur1.next;
            }else{
                cur2.next = cur;
                cur2 = cur2.next;
            }
            cur = cur.next;
        }
        if(cur2 == head2) return head1.next;
        cur2.next = null;
        cur1.next = head2.next;
        return head1.next;
    }

    public void reversePrint(ListNode head){
        if(head != null){
            reversePrint(head.next);
            System.out.print(head.val + " ");
        }
    }

    public void reversePrintByStack(ListNode head){
        ListNode cur = head;
        Stack<ListNode> stack = new Stack<>();
        while(cur != null){
            stack.push(cur);
            cur = cur.next;
        }
        while(!stack.isEmpty()){
            ListNode top = stack.pop();
            System.out.print(top.val + " ");
        }
        System.out.println();
    }
}
